Use Context in Article::delete for messages
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTestCase.php
1 <?php
2
3 abstract class ApiTestCase extends MediaWikiLangTestCase {
4 protected static $apiUrl;
5
6 /**
7 * @var ApiTestContext
8 */
9 protected $apiContext;
10
11 /**
12 * @var array
13 */
14 protected $tablesUsed = array( 'user', 'user_groups', 'user_properties' );
15
16 protected function setUp() {
17 global $wgServer;
18
19 parent::setUp();
20 self::$apiUrl = $wgServer . wfScript( 'api' );
21
22 ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
23
24 self::$users = array(
25 'sysop' => new TestUser(
26 'Apitestsysop',
27 'Api Test Sysop',
28 'api_test_sysop@example.com',
29 array( 'sysop' )
30 ),
31 'uploader' => new TestUser(
32 'Apitestuser',
33 'Api Test User',
34 'api_test_user@example.com',
35 array()
36 )
37 );
38
39 $this->setMwGlobals( array(
40 'wgMemc' => new EmptyBagOStuff(),
41 'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ),
42 'wgRequest' => new FauxRequest( array() ),
43 'wgUser' => self::$users['sysop']->user,
44 ) );
45
46 $this->apiContext = new ApiTestContext();
47 }
48
49 /**
50 * Edits or creates a page/revision
51 * @param string $pageName Page title
52 * @param string $text Content of the page
53 * @param string $summary Optional summary string for the revision
54 * @param int $defaultNs Optional namespace id
55 * @return array Array as returned by WikiPage::doEditContent()
56 */
57 protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
58 $title = Title::newFromText( $pageName, $defaultNs );
59 $page = WikiPage::factory( $title );
60
61 return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
62 }
63
64 /**
65 * Does the API request and returns the result.
66 *
67 * The returned value is an array containing
68 * - the result data (array)
69 * - the request (WebRequest)
70 * - the session data of the request (array)
71 * - if $appendModule is true, the Api module $module
72 *
73 * @param array $params
74 * @param array|null $session
75 * @param bool $appendModule
76 * @param User|null $user
77 *
78 * @return array
79 */
80 protected function doApiRequest( array $params, array $session = null,
81 $appendModule = false, User $user = null
82 ) {
83 global $wgRequest, $wgUser;
84
85 if ( is_null( $session ) ) {
86 // re-use existing global session by default
87 $session = $wgRequest->getSessionArray();
88 }
89
90 // set up global environment
91 if ( $user ) {
92 $wgUser = $user;
93 }
94
95 $wgRequest = new FauxRequest( $params, true, $session );
96 RequestContext::getMain()->setRequest( $wgRequest );
97
98 // set up local environment
99 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
100
101 $module = new ApiMain( $context, true );
102
103 // run it!
104 $module->execute();
105
106 // construct result
107 $results = array(
108 $module->getResultData(),
109 $context->getRequest(),
110 $context->getRequest()->getSessionArray()
111 );
112
113 if ( $appendModule ) {
114 $results[] = $module;
115 }
116
117 return $results;
118 }
119
120 /**
121 * Add an edit token to the API request
122 * This is cheating a bit -- we grab a token in the correct format and then
123 * add it to the pseudo-session and to the request, without actually
124 * requesting a "real" edit token.
125 *
126 * @param array $params Key-value API params
127 * @param array|null $session Session array
128 * @param User|null $user A User object for the context
129 * @return array Result of the API call
130 * @throws Exception In case wsToken is not set in the session
131 */
132 protected function doApiRequestWithToken( array $params, array $session = null,
133 User $user = null
134 ) {
135 global $wgRequest;
136
137 if ( $session === null ) {
138 $session = $wgRequest->getSessionArray();
139 }
140
141 if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
142 // @todo Why does this directly mess with the session? Fix that.
143 // add edit token to fake session
144 $session['wsEditToken'] = $session['wsToken'];
145 // add token to request parameters
146 $timestamp = wfTimestamp();
147 $params['token'] = hash_hmac( 'md5', $timestamp, $session['wsToken'] ) .
148 dechex( $timestamp ) .
149 User::EDIT_TOKEN_SUFFIX;
150
151 return $this->doApiRequest( $params, $session, false, $user );
152 } else {
153 throw new Exception( "Session token not available" );
154 }
155 }
156
157 protected function doLogin( $user = 'sysop' ) {
158 if ( !array_key_exists( $user, self::$users ) ) {
159 throw new MWException( "Can not log in to undefined user $user" );
160 }
161
162 $data = $this->doApiRequest( array(
163 'action' => 'login',
164 'lgname' => self::$users[$user]->username,
165 'lgpassword' => self::$users[$user]->password ) );
166
167 $token = $data[0]['login']['token'];
168
169 $data = $this->doApiRequest(
170 array(
171 'action' => 'login',
172 'lgtoken' => $token,
173 'lgname' => self::$users[$user]->username,
174 'lgpassword' => self::$users[$user]->password,
175 ),
176 $data[2]
177 );
178
179 return $data;
180 }
181
182 protected function getTokenList( $user, $session = null ) {
183 $data = $this->doApiRequest( array(
184 'action' => 'tokens',
185 'type' => 'edit|delete|protect|move|block|unblock|watch'
186 ), $session, false, $user->user );
187
188 if ( !array_key_exists( 'tokens', $data[0] ) ) {
189 throw new MWException( 'Api failed to return a token list' );
190 }
191
192 return $data[0]['tokens'];
193 }
194
195 public function testApiTestGroup() {
196 $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
197 $constraint = PHPUnit_Framework_Assert::logicalOr(
198 $this->contains( 'medium' ),
199 $this->contains( 'large' )
200 );
201 $this->assertThat( $groups, $constraint,
202 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
203 );
204 }
205 }